﻿var express = require('express');
var router = express.Router();
var connection = require('../backjs/node_mysql.js')

router.post('/project_list', function (req, res) {
    if (req.body.requestType == 'client') {
        var sql = 'select pj_id,pj_name,pj_state,pj_start,pj_fin from project where pj_client = ? order by pj_id DESC';
        var params = [req.body.pj_client];
        /*  if (req.body.sort) {
              sql += "pj_name like %xx% and pj_start>=req.body.start and pj_start<=req.body.fin";
  
          }*/
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
    if (req.body.requestType == 'staff') {//业务部门？？？
        var sql = 'select pj_id,pj_name,pj_state,pj_start,pj_fin,pj_company,user_name from project left join user on user_id = pj_client order by pj_id DESC';
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                res.send(rs);
            }
        });
    }
});

//设置每一页的行数
router.post('/countPerPage',function (req,res) {
    req.session.user.countPerPage =  req.body.rowsNumbuer;
    res.send(200);
});

//设置用户同时同步公司
router.post('/company', function (req, res) {
    var sql = 'update project set pj_client = ?, pj_company = ? where pj_id = ?';
    var params = [req.body.pj_client, req.body.pj_company, req.body.pj_id];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            res.send(200);
        }

    });
});

//项目详情
router.post('/checkDepute', function (req, res) {
    if (req.body.requestType == 'initBaseData') {
        var sql = 'select project.*, user_name from project left join user on user_id = pj_client where pj_id = ?'
        var params = [req.body.pj_id];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            else {
                console.log('项目编号为 ' + req.body.pj_id + ' 的数据为： ' + rs[0]);
                res.send(rs);
            }
        })
    }
    else if (req.body.requestType == 'initTaskData') {
        //是否需要委托单列表？？？
    }
    else if (req.body.requestType == 'update') {
        var sql = 'update project set pj_state = ?,pj_fin = ? where pj_id = ?';
        var parms = [req.body.pj_state, req.body.pj_fin];
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err;
            }
            res.send(200);
        });
    }
});

//新建项目
router.post('/newProject', function (req, res) {
    //初始化
    var sql = 'insert into project (pj_uid,pj_name,pj_start,pj_fin,pj_company,pj_client,pj_addr,pj_description) values (?)';
    var params = [[req.body.pj_uid, req.body.pj_name, req.body.pj_start, req.body.pj_fin, req.body.pj_company, req.body.pj_client, req.body.pj_addr, req.body.pj_description]];

    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err;
        }
        else {
            console.log(rs)
            res.send(rs);
        }
    });
});

module.exports = router;

